<!-- 商品销售分析表 -->
<template>
  <div class="mod-config">
    <basic-container>
      <el-divider content-position="left">查询条件</el-divider>
      <el-form size="mini" label-width="80px">
        <el-row class="row-bg">
          <el-form-item label="分析角度">
            <el-radio-group v-model="spreadData.classify">
              <el-radio label="1">按商品</el-radio>
              <el-radio label="2">按套餐</el-radio>
              <el-radio label="3">按销售同期</el-radio>
              <el-radio label="5">按商品属性</el-radio>
              <el-radio label="6">按客单价</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>
        <div v-if="spreadData.classify==1">
          <el-row class="row-bg">
            <el-form-item label="查询分类">
              <el-radio-group v-model="spreadData.checkfly">
                <el-radio label="1">商品类别</el-radio>
                <el-radio label="2">按制造商</el-radio>
                <el-radio label="3">按商品品种</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
        </div>
        <div v-if="spreadData.classify==1">
          <el-row class="row-bg">
            <el-form-item label="销售信息">
              <el-radio-group v-model="dataForm.tab1">
                <el-radio @click.native.prevent="clickitem1(0)" :label="0">销售数量</el-radio>
              </el-radio-group>
              <el-radio-group v-model="dataForm.tab2">
                <el-radio @click.native.prevent="clickitem2(0)" :label="0">退货数量</el-radio>
              </el-radio-group>
              <el-radio-group v-model="dataForm.tab3">
                <el-radio @click.native.prevent="clickitem3(0)" :label="0">实售数量</el-radio>
              </el-radio-group>
              <el-radio-group v-model="dataForm.tab4">
                <el-radio @click.native.prevent="clickitem4(0)" :label="0">销售金额</el-radio>
              </el-radio-group>
              <el-radio-group v-model="dataForm.tab5">
                <el-radio @click.native.prevent="clickitem5(0)" :label="0">退款金额</el-radio>
              </el-radio-group>
              <el-radio-group v-model="dataForm.tab6">
                <el-radio @click.native.prevent="clickitem6(0)" :label="0">实售金额</el-radio>
              </el-radio-group>
              <el-radio-group v-model="dataForm.tab7">
                <el-radio @click.native.prevent="clickitem7(0)" :label="0">金额占比</el-radio>
              </el-radio-group>
              <el-radio-group v-model="dataForm.tab8">
                <el-radio @click.native.prevent="clickitem8(0)" :label="0">成本金额</el-radio>
              </el-radio-group>
              <el-radio-group v-model="dataForm.tab9">
                <el-radio @click.native.prevent="clickitem9(0)" :label="0">成本占比</el-radio>
              </el-radio-group>
              <el-radio-group v-model="dataForm.tab10">
                <el-radio @click.native.prevent="clickitem10(0)" :label="0">毛利金额</el-radio>
              </el-radio-group>
              <el-radio-group v-model="dataForm.tab11">
                <el-radio @click.native.prevent="clickitem11(0)" :label="0">销售毛利率</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
        </div>


        <div v-if="spreadData.classify==3">
          <el-row class="row-bg">
            <el-form-item label="查询分类">
              <el-radio-group v-model="spreadData.checkfly3">
                <el-radio label="1">按商品类别</el-radio>
                <el-radio label="2">按门店</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
        </div>
        <div v-if="spreadData.classify==5">
          <el-row class="row-bg">
            <el-form-item label="查询角度">
              <el-radio-group v-model="spreadData.seltype">
                <el-radio label="1">按门店</el-radio>
                <el-radio label="2">按营业员</el-radio>
                <el-radio label="3">按验光师</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
        </div>
        <div v-if="spreadData.classify == '5'">
          <el-row class="row-bg">
            <el-form-item label="查询分类">
              <el-radio-group v-model="spreadData.checkfly5" @change="checkfly55Change">
                <el-radio label="1">镜架</el-radio>
                <el-radio label="2">镜片</el-radio>
                <el-radio label="3">隐形</el-radio>
                <el-radio label="4">太阳镜</el-radio>
                <el-radio label="5">配件</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>

          <el-row class="row-bg" v-if="spreadData.checkfly5 == '1'">
            <el-form-item label="查询分类2">
              <el-radio-group v-model="spreadData.checkfly55">
                <el-radio label="1">按镜架材质</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
          <el-row class="row-bg" v-if="spreadData.checkfly5 == '2'">
            <el-form-item label="查询分类2">
              <el-radio-group v-model="spreadData.checkfly55">
                <el-radio label="2">按镜片折射率</el-radio>
                <el-radio label="3">按镜片镜片分类</el-radio>
                <el-radio label="4">按镜片镜片功能</el-radio>
                <el-radio label="5">按镜片渐进片分类</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
          <el-row class="row-bg" v-if="spreadData.checkfly5 == '3'">
            <el-form-item label="查询分类2">
              <el-radio-group v-model="spreadData.checkfly55">
                <el-radio label="6">按隐形使用类型</el-radio>
                <el-radio label="7">按隐形抛弃型</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
          <el-row class="row-bg" v-if="spreadData.checkfly5 == '4'">
            <el-form-item label="查询分类2">
              <el-radio-group v-model="spreadData.checkfly55">
                <el-radio label="8">按太阳镜功能</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
          <el-row class="row-bg" v-if="spreadData.checkfly5 == '5'">
            <el-form-item label="查询分类2">
              <el-radio-group v-model="spreadData.checkfly55">
                <el-radio label="9">按配件类型</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
        </div>
        <div v-if="spreadData.classify == '6'">
          <el-row class="row-bg">
            <el-form-item label="销售类型">
              <el-select v-model="dataForm.saleType" clearable>
                <el-option value="4" label="框镜成品"></el-option>
                <el-option value="2" label="框镜订做"></el-option>
                <el-option value="3" label="隐形成品"></el-option>
                <el-option value="1" label="隐形订做"></el-option>
                <el-option value="5" label="辅料"></el-option>
              </el-select>
            </el-form-item>
          </el-row>
        </div>
        <el-row class="row-bg">
          <el-form-item label="所属公司" v-if="[1,21,23].includes(userInfo.lessDeptId)">
            <el-select size="mini" style="width: 175px;" v-model="spreadData.unitId" @change="unitChangeHandle">
              <el-option v-for="item in companyOption" :value="item.id" :label="item.name" :key='item.id'></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属公司" v-else>
            <el-select size="mini" style="width: 175px;" v-model="spreadData.unitId" disabled>
              <el-option v-for="item in companyOption" :value="item.id" :label="item.name" :key='item.id'></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row class="row-bg">
          <el-col :span="9">
            <el-form-item label="查询部门">
              <el-input v-model="spreadData.deptName" v-if="userInfo.lessDeptId != '1' && userInfo.lessDeptId != '21' && userInfo.lessDeptId != '23'" disabled style="width: 175px;"></el-input>
              <el-input size="mini" type="textarea" v-model="spreadData.deptName" v-if="[1,21,23].includes(userInfo.lessDeptId)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label-width="0" v-if="[1,21,23].includes(userInfo.lessDeptId)">
              <el-button size="mini" @click="openDeptDialog">选择</el-button>
            </el-form-item>
          </el-col>

          <div v-if="spreadData.classify!=3">
            <el-col :span="12">
              <el-form-item label="查询日期">
                <el-col :span="11">
                  <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.beginTime" value-format="yyyy-MM-dd"
                    style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line" :span="2" style="text-align: center;">至</el-col>
                <el-col :span="11">
                  <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.endTime" value-format="yyyy-MM-dd"
                    style="width: 100%;"></el-date-picker>
                </el-col>
              </el-form-item>
            </el-col>
          </div>
          <div v-if="spreadData.classify==3">
            <el-col :span="12">
              <el-form-item label="本期日期">
                <el-col :span="11">
                  <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.beginTime" value-format="yyyy-MM-dd"
                    style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line" :span="2" style="text-align: center;">至</el-col>
                <el-col :span="11">
                  <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.endTime" value-format="yyyy-MM-dd"
                    style="width: 100%;"></el-date-picker>
                </el-col>
              </el-form-item>
              <el-form-item label="同期日期">
                <el-col :span="11">
                  <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.tqbeginTime" value-format="yyyy-MM-dd"
                    style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line" :span="2" style="text-align: center;">至</el-col>
                <el-col :span="11">
                  <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.tqendTime" value-format="yyyy-MM-dd"
                    style="width: 100%;"></el-date-picker>
                </el-col>
              </el-form-item>
            </el-col>
          </div>
        </el-row>
        <div v-if="spreadData.classify==1">
          <el-row class="row-bg">
            <el-form-item label="查询分类">
              <el-radio-group v-model="spreadData.checkdept">
                <el-radio label="1">显示</el-radio>
                <el-radio label="2">不显示</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
            <el-row class="row-bg">
              <el-col :span="12">
                <el-form-item label="商品类别">
                  <el-select v-model="dataForm.ctgId" @change="changeCtgId" clearable>
                    <el-option v-for="item in ctgOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="制造商">
                  <el-input v-model="dataForm.supplyName" @change="changeSupplyId">
                    <el-button slot="append" @click="openSupplyDialog">选择</el-button>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row class="row-bg">
              <el-col :span="8">
                <el-form-item label="商品品种">
                  <el-input v-model="dataForm.brandName" readonly="readonly">
                    <el-button slot="append" @click="getBrandList">选择</el-button>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="商品代码">
                  <el-input size="mini" v-model="dataForm.goodsCode" style="width: 175px;"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="商品名称">
                  <el-input size="mini" v-model="dataForm.goodsName" style="width: 175px;"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
        </div>
        <el-row class="row-bg">
          <el-col :span="8">
            <el-form-item label="显示查询条件" label-width="100px">
              <el-radio-group v-model="spreadData.condition">
                <el-radio label="1">是</el-radio>
                <el-radio label="2">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top: 10px;">
          <el-form-item label-width="5px">
            <el-link :href="printUrl" target="_blank" type="primary">
              <el-button size="mini" type="primary" @click="getWorkParams">查 询</el-button>
            </el-link>
            <el-button type="info" icon="el-icon-delete" style="margin-left: 15px;">清 空</el-button>
          </el-form-item>
        </el-row>

      </el-form>
    </basic-container>
    <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData" :company="this.spreadData.unitId"></search-dept>
    <!-- 弹窗，查询制造商 -->
    <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
    <!-- 弹窗, 查询商品品种-->
    <search-brand v-if="brandVisible" ref="selectBrand" @refreshData="getBrandData"></search-brand>
  </div>
</template>

<script>
  let params = '/saleFXB/sales_goodsSalesAnalysisNew_category2.cpt'
  import { getDateFormat, filterParams } from '@/util/tools'
  import { getCtgList } from '@/api/eims/ctg'
  import { unitList } from '@/api/admin/unit'
  import { getDeptPage } from '@/api/admin/dept'
  import { mapGetters } from 'Vuex'

  import SearchDept from '@/components/business/SearchDept'
  import SearchSupply from '@/components/business/SearchSupply'
  import SearchBrand from '@/components/business/SearchBrand'

  export default {
    data() {
      return {
        radio: 3,
        date1: '',
        date2: '',
        printUrl: '',
        ctgOptions: [],
        companyOption: [],
        deptVisible: false,
        supplyVisible: false,
        brandVisible: false,
        spreadData: {
          classify: '1',    //  分析角度
          checkfly: '1',    //  查询分类-按商品
          checkfly3: '1',   //  查询分类-按销售同期
          checkfly5: '1',   //  查询分类-按商品属性
          checkfly55: '1',
          checkdept:'2',    //查询分类-展示部门
          seltype: '1',     //  查询角度
          mode: '2',        //  查询方式
          checkType: '1',   //  查看方式
          unitId: '',
          deptName: '',     //  查询部门Name
          condition: '2',   //  是否显示查询条件
        },
        dataForm: {
          supplyFlag: '', //  有无供应商传值
          deptId: '', //  查询部门ID
          ctgId: '',
          saleType: '',
          beginTime: '', //  查询开始时间
          endTime: '', //  查询结束时间
          tqbeginTime: '', //  查询同期开始时间
          tqendTime: '', //  查询同期结束时间
          jobNumber: '', //  工号
          goodsCode: '',
          goodsName:'',
            tab1: 0,
            tab2: 0,
            tab3: 0,
            tab4: 0,
            tab5: 0,
            tab6: 0,
            tab7: 0,
            tab8: 0,
            tab9: 0,
            tab10: 0,
            tab11: 0,

        }
      }
    },
    components:{
      SearchDept,
      SearchSupply,
      SearchBrand
    },
    computed: {
      ...mapGetters(['userInfo', 'bizData'])
    },
    created() {
      this.dataForm.beginTime = getDateFormat(new Date())
      this.dataForm.endTime = getDateFormat(new Date())
      this.dataForm.tqbeginTime = getDateFormat(new Date())
      this.dataForm.tqendTime = getDateFormat(new Date())
      this.getFormData()
      this.getCtgOptions()
      this.getCompanyList()
      this.spreadData.unitId = this.userInfo.unitId
      this.spreadData.deptId = [this.userInfo.lessDeptId]
      this.spreadData.deptName = this.userInfo.lessDeptName
    },
    methods: {
      getFormData() {
        this.dataForm.deptId = this.userInfo.lessDeptId
        this.spreadData.deptName = this.userInfo.lessDeptName
      },
      unitChangeHandle(val) {
        let ids = []
        let names = []
        getDeptPage({current: 1,size: 100,unitId: val}).then(result => {
          let resultData = result.data.data.records

          resultData.forEach(item => {
            ids.push(item.id)
            names.push(item.name)
          })
          this.spreadData.deptName = names.join(',')
          this.spreadData.deptId = ids
          this.dataForm.deptId = ids
        })
      },
      //  打开选择制造商弹窗
      openSupplyDialog() {
          // if (!this.dataForm.ctgId) {
          //     this.$message.warning('请选择商品类别')
          //     return false
          // }
          let data = {  
              ctgId: this.dataForm.ctgId || null
          }
          this.supplyVisible = true
          this.$nextTick(() => {
              this.$refs.supplyDialog.init(data)
          })
      },
      changeSupplyId(){
          this.dataForm.brandId = ''
          this.dataForm.brandName= ''
      },
      changeCtgId(){

      },
      getWorkParams() {
        if (this.spreadData.classify == '1' && this.spreadData.checkfly == '1') {
            if(this.spreadData.checkdept == '1'){
                params = '/saleFXB/sales_goodsSalesAnalysisNew_category.cpt'
            }else{
                params = '/saleFXB/sales_goodsSalesAnalysisNew_category2.cpt'
            }
        } else if (this.spreadData.classify == '1' && this.spreadData.checkfly == '2') {
            if(this.spreadData.checkdept == '1'){
                params = '/saleFXB/sales_goodsSalesAnalysisNew_supplier.cpt'
            }else{
                params = '/saleFXB/sales_goodsSalesAnalysisNew_supplier2.cpt'
            }
        } else if (this.spreadData.classify == '1' && this.spreadData.checkfly == '3') {
            if(this.spreadData.checkdept == '1'){
                params = '/saleFXB/sales_goodsSalesAnalysisNew_brand.cpt'
            }else{
                params = '/saleFXB/sales_goodsSalesAnalysisNew_brand2.cpt'
            }
          this.dataForm.supplyFlag = 1;
        } else if (this.spreadData.classify == '2') {
          params = '/saleFXB/sales_salesSetMealBySum.cpt'
        } else if (this.spreadData.classify == '3' && this.spreadData.checkfly3 == '1') {
          params = '/saleFXB/sales_goodsSametimeAnalysis.cpt'
        } else if (this.spreadData.classify == '3' && this.spreadData.checkfly3 == '2') {
          params = '/saleFXB/sales_salesShopSametimeAnalysis.cpt'
        } else if (this.spreadData.classify == '5' && this.spreadData.checkfly5 == '1') {
          params = '/saleFXB/sales_goodsSalesAnalysis_frame.cpt'
        } else if (this.spreadData.classify == '5' && this.spreadData.checkfly5 == '2') {
          params = '/saleFXB/sales_goodsSalesAnalysis_frame2.cpt'
        } else if (this.spreadData.classify == '5' && this.spreadData.checkfly5 == '3') {
          params = '/saleFXB/sales_goodsSalesAnalysis_frame3.cpt'
        } else if (this.spreadData.classify == '6') {
          params = '/saleFXB/sales_salesForkdjRpt.cpt'
        }
        let data = filterParams(this.dataForm)
        let parameter = ''
        for (let key in data) {
          parameter += '&' + key + '=' + this.dataForm[key]
        }
        // let parameter = '&deptId='+this.dataForm.deptId + '&beginTime='+this.dataForm.beginTime+ '&endTime='+this.dataForm.endTime
        //let frUrl = "http://localhost:8075/webroot/decision/view/report?viewlet="
        //this.printUrl = frUrl + params + parameter
        this.printUrl = this.bizData.printUrl + params + parameter
      },
      //  查询商品类别列表
      getCtgOptions() {
        getCtgList().then(result => {
          this.ctgOptions = result.data.data
        })
      },
      //  查询公司列表
      getCompanyList() {
        unitList().then(result => {
          this.companyOption = result.data.data
        })
      },
      //  打开查询部门弹窗
      openDeptDialog() {
        this.deptVisible = true
        this.$nextTick(() => {
          this.$refs.deptDialog.init()
        })
      },
      getSupplyData(data){
          this.$set(this.dataForm,'supplyId',data.id)
          this.$set(this.dataForm,'supplyName',data.name)
      },
      // 获取商品品种信息
      getBrandList() {
          // if (!this.dataForm.ctgId) {
          //     this.$message.warning('请选择商品类别')
          //     return false
          // }
          if (!this.dataForm.supplyId) {
              this.$message.warning('请选择制造商')
              return false
          }
          let data = {
              ctgId: this.dataForm.ctgId || null,
              supplyId: this.dataForm.supplyId
          }
          this.brandVisible = true
          this.$nextTick(() => {
              this.$refs.selectBrand.init(data)
          })
      },
      getBrandData(data){
          this.$set(this.dataForm,'brandId',data.id)
          this.$set(this.dataForm,'brandName',data.name)
      },
      //  获取选择部门数据
      getDeptData(data) {
        let ids = []
        let names = []
        if(data.length){
          data.forEach(item => {
            ids.push(item.id)
            names.push(item.name)
          })
        }
        this.spreadData.deptName = names.join(',')
        this.spreadData.deptId = ids
        this.dataForm.deptId = ids
      },

      clickitem1 (e) {
        console.log(e)
            e === this.dataForm.tab1 ? this.dataForm.tab1 = '1' : this.dataForm.tab1 = e,

            console.log(this.dataForm.tab1)
        },
      clickitem2 (e) {
          e === this.dataForm.tab2 ? this.dataForm.tab2 = '1' : this.dataForm.tab2 = e
      },
      clickitem3 (e) {
          e === this.dataForm.tab3 ? this.dataForm.tab3 = '1' : this.dataForm.tab3 = e
      },
      clickitem4 (e) {
          e === this.dataForm.tab4 ? this.dataForm.tab4 = '1' : this.dataForm.tab4 = e
      },
      clickitem5 (e) {
          e === this.dataForm.tab5 ? this.dataForm.tab5 = '1' : this.dataForm.tab5 = e
      },
      clickitem6 (e) {
          e === this.dataForm.tab6 ? this.dataForm.tab6= '1' : this.dataForm.tab6 = e
      },
      clickitem7 (e) {
          e === this.dataForm.tab7 ? this.dataForm.tab7 = '1' : this.dataForm.tab7 = e
      },
      clickitem8 (e) {
          e === this.dataForm.tab8 ? this.dataForm.tab8 = '1' : this.dataForm.tab8 = e
      },
      clickitem9 (e) {
          e === this.dataForm.tab9 ? this.dataForm.tab9 = '1' : this.dataForm.tab9 = e
      },
      clickitem10 (e) {
          e === this.dataForm.tab10 ? this.dataForm.tab10 = '1' : this.dataForm.tab10 = e
      },
      clickitem11 (e) {
          e === this.dataForm.tab11 ? this.dataForm.tab11 = '1' : this.dataForm.tab11 = e
      },
      checkfly55Change(val) {
        switch(val){
          case '1' :
            this.spreadData.checkfly55 = '1'
            break
          case '2' :
            this.spreadData.checkfly55 = '2'
            break
          case '3' :
            this.spreadData.checkfly55 = '6'
            break
          case '4' :
            this.spreadData.checkfly55 = '8'
            break
          case '5' :
            this.spreadData.checkfly55 = '9'
            break

        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .row-bg {
    background-color: #cadee8;
    // padding-left: 15px;
    padding-right: 15px;
    padding-top: 5px;
    margin-bottom: 2px;
  }
</style>
